package edu.ptit.jad.preprocess.app;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.TreeSet;

import edu.ptit.jad.configuration.CONSTANTS;
import edu.ptit.jad.data.DataAccess;
import edu.ptit.jad.exception.AdException;
import edu.ptit.jad.model.Ad;

public class AppGenAd {

	static SortedSet<String> sortedset = new TreeSet<String>();

	public static void main(String[] args) throws Exception {

		File file = new File("data/trains/adcat.txt");
		String line = "";
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				new FileInputStream(file), "UTF8"));

		while ((line = reader.readLine()) != null) {

			if (line.equals(""))
				continue;

			String[] row = line.split(":");

			StringTokenizer tknr = new StringTokenizer(row[1].substring(0,
					row[1].length() - 1), ",");
			// System.err.println(row[1].substring(0, row[1].length() - 1));
			while (tknr.hasMoreTokens()) {

				// allTermsSet.add(tknr.nextToken());
				sortedset.add(tknr.nextToken().trim());
			}

		}

		//saveVocabs("data/trains/cats.jad");
	}

	public static void saveVocabs(String fileDir) throws AdException {

		try {
			File file = new File(fileDir);
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(file), "UTF8"));

			Iterator<String> it = sortedset.iterator();
			String term = "";
			while (it.hasNext()) {

				term = it.next();
				System.out.println(term);
				// out.write(term);
				// out.write("\n");

			}

		} catch (Exception e) {

			e.printStackTrace();
			throw new AdException("File write error " + fileDir);
		}
	}
}
